// 账户管理
var Account = Ext.data.Record.create([
	{name : 'tid',mapping : 'tid',type : 'int'}, 
	{name : 'fid',mapping : 'fid',type : 'int'}, 
	{name : 'username',mapping : 'username',type : 'string'}, 
	{name : 'cardnumber',mapping : 'cardnumber',type : 'string'}, 
	{name : 'bank',mapping : 'bank',type : 'int'},  
	{name : 'bankname',mapping : 'bankname',type : 'string'}, 
	{name : 'cardtype',mapping : 'cardtype',type : 'int'},  
	{name : 'typename',mapping : 'typename',type : 'string'}, 
	{name : 'owner',mapping : 'owner',type : 'int'},
	{name : 'name',mapping : 'name',type : 'string'},
	{name : 'balance',mapping : 'balance',type : 'float'},
	{name : 'createbalance',mapping : 'createbalance',type : 'float'},
	{name : 'memo',mapping : 'memo',type : 'string'}
	]);

var cm_account = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
	header : '开户人',
	width : 60,
	dataIndex : 'username'
}, {
	header : '所属投资单位',
	width : 90,
	dataIndex : 'name',
	sortable : true
},{
	header : '卡号',
	width : 80,
	dataIndex : 'cardnumber',
	sortable : false
},{
	header : '所属银行',
	width : 90,
	dataIndex : 'bankname',
	sortable:true
	
},{
	header : '卡类型',
	width : 60,
	dataIndex : 'typename',
	sortable:true
	
},{
	header : '初始余额',
	width : 60,
	dataIndex : 'createbalance',
	sortable : true
}]);

cm_account.defaultSortable = true;

var btn_add_account = new Ext.Button({
	text : '添加账户',
	iconCls : 'icon-add',
	handler : function() {
		window_add_account.show();
	}
});

var btn_edit_account = new Ext.Button({
	text : '修改账户',
	iconCls : 'icon-edit',
	handler : function() {
		var record = grid_account.getSelectionModel().getSelected();
		if (record) {
			window_edit_account.show();
		}else{
			Ext.Msg.alert('提示','请选择要修改的账户!');
		}
	}
});

var btn_del_account = new Ext.Button({
	text : '删除账户',
	iconCls : 'icon-del',
	handler : function() {
		var record = grid_account.getSelectionModel().getSelected();
		if (record) {
			Ext.Msg.confirm('确认删除', '你确定删除该条记录?', function(btn) {
				if (btn == 'yes') {
					Ext.Ajax.request({
						url : '/account/deleteAccount.action',
						params : {tid : record.data.tid},
						success : function() {ds_account.reload();},
						failure : function() {
							Ext.Msg.show({
								title : '错误提示',
								msg : '删除时发生错误,您可能不是管理员!',
								buttons : Ext.Msg.OK,
								icon : Ext.Msg.ERROR
							});
						}
					});
				}
			});
		}
	}
});

var text_search_account = new Ext.form.TextField({
	name : 'textSearchAccount',
	width : 200,
	emptyText : '根据卡号模糊查询!',
	listeners : {
		'specialkey' : function(field, e) {
			if (e.getKey() == Ext.EventObject.ENTER) {
				searchAccount();
			}
		}
	}
});

var searchAccount = function() {
	ds_account.baseParams.conditions = text_search_account.getValue(); 
	ds_account.load({params : {start : 0,limit : 20}});
}

var btn_search_account = new Ext.Button({
	text : '查询',
	iconCls : 'icon-search',
	handler : searchAccount
});

var ds_account = new Ext.data.Store({
	url : '/account/findAllAccount.action',
	reader : new Ext.data.JsonReader({totalProperty : 'totalProperty',root : 'root'}, 
	[{name : 'tid',type : 'int'}, 
	{name : 'username',type : 'string'}, 
	{name : 'name',type : 'string'}, 
	{name : 'cardnumber',type : 'string'}, 
	{name : 'bank',type : 'int'},
	{name : 'bankname',type : 'string'}, 
	{name : 'cardtype',type : 'string'},
	{name : 'typename',type : 'string'},
	{name : 'owner',type : 'int'},
	{name : 'balance',type : 'float'},
	{name : 'createbalance',type : 'float'}
	])
});

var grid_account = new Ext.grid.EditorGridPanel({
	title : '账户管理',
	iconCls : 'icon-grid',
	loadMask : {msg : '数据加载中...'},
	width:600,
	region : 'center',
	cm : cm_account,
	ds : ds_account,
	sm : new Ext.grid.RowSelectionModel({singleSelect : true}),
	enableColumnMove : false,
	trackMouseOver : false,
	frame : true,
	clicksToEdit : 1,
	tbar : [btn_add_account,'-',btn_edit_account, '-', btn_del_account, '-',text_search_account, btn_search_account],
	bbar : new Ext.PagingToolbar({
		pageSize : 20,
		store : ds_account,
		displayInfo : true,
		displayMsg : '第 {0} - {1} 条  共 {2} 条',
		emptyMsg : "没有记录",
		items:[{   
                text: '导出到Excel',   
                tooltip: 'Excel',   
                handler: accountExport // 
                        }]
	})
});

function accountExport(){
	doExport(grid_account);
}

var window_add_account = new Ext.Window({
	title : '添加账户',
	width : 320,
	resizable : false,
	autoHeight : true,
	modal : true,
	closeAction : 'hide',
	listeners : {
		'hide' : function() {
			this.setTitle('添加账户');
			this.findById('account.cardnumber').ownerCt.form.reset();
		}
	},
	items : [new Ext.FormPanel({
		border : false,
		baseCls : 'x-plain',
		bodyStyle : 'padding:5px 5px 0',
		labelAlign : 'right',
		labelWidth : 70,
		defaults : {anchor : '93%',msgTarget : 'side'},
		defaultType : 'textfield',
		items : [{
			fieldLabel : '开户人',
			id : 'account.username',
			name : 'account.username',
			hiddenName : 'account.username',
			allowBlank : false,
			minLength : 2,
			maxLength : 10
		}, {
		   	xtype : 'combo',
			fieldLabel : '所属单位',
			name : 'cardtype',
			hiddenName : 'account.owner',
			valueField : 'tid',
		    displayField : 'name',
		    mode : 'remote',
			store : ds_debtor_select,
			selectOnFocus : true,
			editable : false,
			allowBlank : false,
			triggerAction : 'all',
			loadingText : '加载中...',
			emptyText : '请选择所属单位',
			listeners : {
				'select' : function(combo, record, index) {
					this.ownerCt.ownerCt.form.findField('account.name').setValue(record.data.name);
				},
				'expand' : function(){
					ds_debtor_select.load();
				}
			}
		   }, {xtype : 'hidden',name : 'account.name'}, 
		{
		 	fieldLabel : '卡号',
		 	name : 'account.cardnumber',
		 	id : 'account.cardnumber',
		 	allowBlank : false,
		 	minLength : 10,
		 	maxLength : 30
		  },
		  {
		  	xtype:'numberfield',
		 	fieldLabel : '初始金额',
		 	name : 'account.createbalance',
		 	allowBlank : false,
		 	minLength : 2,
		 	maxLength : 12
		  },
		   {
		   	xtype : 'combo',
			fieldLabel : '所属银行',
			name : 'cardtype',
			hiddenName : 'account.bank',
			valueField : 'tid',
		    displayField : 'value',
		    mode : 'remote',
			store : ds_bank_select,
			selectOnFocus : true,
			editable : false,
			allowBlank : false,
			triggerAction : 'all',
			loadingText : '加载中...',
			emptyText : '请选择所属银行',
			listeners : {
				'select' : function(combo, record, index) {
					this.ownerCt.ownerCt.form.findField('account.bankname').setValue(record.data.value);
				},
				'expand' : function(){
					ds_bank_select.load();
				}
			}
		   }, {xtype : 'hidden',name : 'account.bankname'}, 
		   {
		   	xtype : 'combo',
			fieldLabel : '银行卡类型',
			name : 'cardtype',
			hiddenName : 'account.cardtype',
			valueField : 'tid',
		    displayField : 'value',
		    mode : 'remote',
			store : ds_cardtype_select,
			selectOnFocus : true,
			editable : false,
			allowBlank : false,
			triggerAction : 'all',
			loadingText : '加载中...',
			emptyText : '请选择银行卡类型',
			listeners : {
				'select' : function(combo, record, index) {
					this.ownerCt.ownerCt.form.findField('account.typename').setValue(record.data.value);
				},
				'expand' : function(){
					ds_cardtype_select.load();
				}
			}
		   }, {xtype : 'hidden',name : 'account.typename'}, 
		   {xtype: 'textarea',
		   	fieldLabel : '备注',
		   name : 'account.memo',
		   maxLength : 60
		   }
		],
		buttonAlign : 'right',
		minButtonWidth : 60,
		buttons : [{
			text : '添加',
			handler : function(btn) {
				var frm = this.ownerCt.ownerCt.form;
				if (frm.isValid()) {
					btn.disable();
					var unfield = frm.findField('account.cardnumber');
					frm.submit({
						waitTitle : '请稍候',
						url : '/account/saveAccount.action',
						waitMsg : '正在提交表单数据,请稍候...',
						success : function(form, action) {
							window_add_account.hide();
							ds_account.load({params : {start : 0,limit : 20}});
							btn.enable();
						},
						failure : function() {
							Ext.Msg.show({
								title : '错误提示',
								msg : '"' + unfield.getValue() + '" ' + '名称可能已经存在!',
								buttons : Ext.Msg.OK,
								fn : function() {
									unfield.focus(true);
									btn.enable();
								},
								icon : Ext.Msg.ERROR
							});
						}
					});
				}
			}
		}, {text : '重置',handler : function() {this.ownerCt.ownerCt.form.reset();}
		}, {text : '取消',handler : function() {
			this.ownerCt.ownerCt.ownerCt.hide();
			ds_account.load({params : {start : 0,limit : 20}});
		}
		}]
	})]
});

var window_edit_account = new Ext.Window({
	title : '修改账户',
	width : 320,
	resizable : false,
	autoHeight : true,
	modal : true,
	closeAction : 'hide',
	listeners : {
		'hide' : function() {
			this.setTitle('修改账户');
			this.findById('accountd.cardnumber').ownerCt.form.reset();
		},
		'show' : function()  {
			var record = grid_account.getSelectionModel().getSelected();
			this.findById('accountd.username').setValue(record.data.username);
			this.findById('debtord').setValue(record.data.name);
			this.findById('accountd.owner').setValue(record.data.owner);
			this.findById('accountd.cardnumber').setValue(record.data.cardnumber);
			this.findById('cardtyped').setValue(record.data.typename);
			this.findById('banktyped').setValue(record.data.bankname);
			this.findById('accountd.bank').setValue(record.data.bank);
			this.findById('accountd.cardtype').setValue(record.data.cardtype);
			this.findById('accountd.memo').setValue(record.data.memo);	
			this.findById('accountd.tid').setValue(record.data.tid);
		}
		
	},
	items : [new Ext.FormPanel({
		border : false,
		baseCls : 'x-plain',
		bodyStyle : 'padding:5px 5px 0',
		labelAlign : 'right',
		labelWidth : 70,
		defaults : {anchor : '93%',msgTarget : 'side'},
		defaultType : 'textfield',
		items : [{xtype : 'hidden',id: 'accountd.tid',name : 'account.tid'}, {
			fieldLabel : '开户人',
			id : 'accountd.username',
			name : 'account.username',
			hiddenName : 'account.username',
			allowBlank : false,
			minLength : 2,
			maxLength : 10
		}, {
		   	xtype : 'combo',
			fieldLabel : '所属单位',
			id : 'debtord',
			name : 'debtor',
			hiddenName : 'account.name',
			valueField : 'name',
		    displayField : 'name',
		    mode : 'remote',
			store : ds_debtor_select,
			
			editable : false,
			allowBlank : false,
			triggerAction : 'all',
			loadingText : '加载中...',
			emptyText : '请选择所属单位',
			listeners : {
				'select' : function(combo, record, index) {
					this.ownerCt.ownerCt.form.findField('accountd.owner').setValue(record.data.tid);
				},
				'expand' : function(){
					
					ds_debtor_select.reload('');
				}
			}
		   }, {xtype : 'hidden',id: 'accountd.owner',name : 'account.owner'}, 
		{
		 	fieldLabel : '卡号',
		 	name : 'account.cardnumber',
		 	id : 'accountd.cardnumber',
		 	allowBlank : false,
		 	minLength : 10,
		 	maxLength : 30
		  },
		   {
		   	xtype : 'combo',
			fieldLabel : '所属银行',
			id : 'banktyped',
			name : 'banktype',
			hiddenName : 'account.bankname',
			valueField : 'value',
		    displayField : 'value',
		    mode : 'remote',
			store : ds_bank_select,
			
			editable : false,
			allowBlank : false,
			triggerAction : 'all',
			loadingText : '加载中...',
			emptyText : '请选择所属银行',
			listeners : {
				'select' : function(combo, record, index) {
					this.ownerCt.ownerCt.form.findField('accountd.bank').setValue(record.data.tid);
				},
				'expand' : function(){
					ds_bank_select.reload('');
				}
			}
		   }, {xtype : 'hidden',id:'accountd.bank',name : 'account.bank'}, 
		   {
		   	xtype : 'combo',
			fieldLabel : '银行卡类型',
			id : 'cardtyped',
			name : 'cardtype',
			hiddenName : 'account.typename',
			valueField : 'value',
		    displayField : 'value',
		    mode : 'remote',
			store : ds_cardtype_select,
			
			editable : false,
			allowBlank : false,
			triggerAction : 'all',
			loadingText : '加载中...',
			emptyText : '请选择银行卡类型',
			listeners : {
				'select' : function(combo, record, index) {
					this.ownerCt.ownerCt.form.findField('accountd.cardtype').setValue(record.data.tid);
				},
				'expand' : function(){
					
					ds_cardtype_select.reload('');
				}
			}
		   }, {xtype : 'hidden',id:'accountd.cardtype',name : 'account.cardtype'}, 
		   {xtype: 'textarea',
		   	fieldLabel : '备注',
		   	id : 'accountd.memo',
		   name : 'account.memo',
		   maxLength : 60
		   }
		],
		buttonAlign : 'right',
		minButtonWidth : 60,
		buttons : [{
			text : '保存',
			handler : function(btn) {
				var frm = this.ownerCt.ownerCt.form;
				if (frm.isValid()) {
					btn.disable();
					var unfield = frm.findField('accountd.cardnumber');
					frm.submit({
						waitTitle : '请稍候',
						url : '/account/updateAccount.action',
						waitMsg : '正在提交表单数据,请稍候...',
						success : function(form, action) {
							window_edit_account.hide();
							ds_account.load({params : {start : 0,limit : 20}});
							btn.enable();
						},
						failure : function() {
							Ext.Msg.show({
								title : '错误提示',
								msg : '"' + unfield.getValue() + '" ' + '名称可能已经存在!',
								buttons : Ext.Msg.OK,
								fn : function() {
									unfield.focus(true);
									btn.enable();
								},
								icon : Ext.Msg.ERROR
							});
						}
					});
				}
			}
		}, {text : '取消',handler : function() {
			this.ownerCt.ownerCt.ownerCt.hide();
			ds_account.load({params : {start : 0,limit : 20}});
		}
		}]
	})]
});


var p_account = {
	id : 'account-panel',
	border : false,
	layout : 'border',
	items : [grid_account]
};